/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package API;

import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author mrbao
 */
public class TabMenu extends Item {

    int background = 0xffffff;
    int bgColor = 0xcccccc;
    int bgFocusedColor = 0x0000ff;
    int foreColor = 0x000000;
    int foreFocusedColor = 0xffffff;
    int padding = 2;
    int margin = 2;
    int scrollStep = 20;
    int selectedTab = 0;
    int[] tabsWidth = null;
    int[] tabsLeft = null;
    int tabHeight = 0;
    Vector tabs = null;
    int menuWidth = 0;
    int viewportWidth = 0;

    public TabMenu(Vector tabs, int width, int x, int y) {
        this.tabs = tabs;
        this.viewportWidth = width;
        this.x = x;
        this.y = y;
        initialize();
    }

    void initialize() {
        tabHeight = font.getHeight() + padding;
        menuWidth = 0;
        tabsWidth = new int[tabs.size()];
        tabsLeft = new int[tabs.size()];
        for (int i = 0; i < tabsWidth.length; i++) {
            tabsWidth[i] = font.stringWidth(tabs.elementAt(i).toString()) + 2 * padding;
            tabsLeft[i] = menuWidth;
            menuWidth += tabsWidth[i];
            if (i > 0) {
                menuWidth += margin;
            }
        }
    }

    public int getHeigh() {
        return tabHeight;
    }

    public void goRight() {
        go(+1);
    }

    public void goLeft() {
        go(-1);
    }

    private void go(int delta) {
        int newTab = Math.max(0, Math.min(tabs.size() - 1, selectedTab + delta));
        boolean scroll = false;
        if (newTab != selectedTab) {
            selectedTab = newTab;
            if (!isTabVisible(newTab)) {
                if ((delta > 0 && tabsLeft[selectedTab] + tabsWidth[selectedTab] > x + viewportWidth)
                        || (delta < 0 && tabsLeft[selectedTab] < x)) {
                    scroll = true;
                    scrollStep = tabsWidth[selectedTab];
                } else {
                    scroll = false;
                }
            }
        }
        if (scroll) {
            x = Math.max(0, Math.min(menuWidth - viewportWidth, x + delta * scrollStep));
        }
    }

    private boolean isTabVisible(int tabIndex) {
        return tabsLeft[tabIndex] + tabsWidth[tabIndex] <= x + viewportWidth && tabsLeft[tabIndex] >= x;
    }

    public void Paint(Graphics g) {
        int currentX = -x;
        g.setColor(background);
        g.setFont(font);
        g.fillRect(x, y, viewportWidth, tabHeight);
        for (int i = 0; i < tabs.size(); i++) {
            g.setColor(i == selectedTab ? bgFocusedColor : bgColor);
            g.fillRoundRect(currentX, y, tabsWidth[i], tabHeight, cornerRadius, cornerRadius);
            g.fillRect(currentX, y + tabHeight / 2, width, tabHeight / 2);
            g.setColor(i == selectedTab ? foreFocusedColor : foreColor);
            g.drawString(tabs.elementAt(i).toString(), currentX + padding, y + padding, Graphics.LEFT | Graphics.TOP);
            currentX += tabsWidth[i];
        }
    }

    public void KeyPress(int gameCode) {
        switch (gameCode) {
            case Canvas.RIGHT:
                goRight();
                break;
            case Canvas.LEFT:
                goLeft();
                break;
        }
    }
}
